package com.netops.bi.provider.util;

import com.aliyun.dingtalknotable_1_0.models.InsertRecordsRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @Author wenlong
 * @Date 2025/9/23 10:26
 * @PackageName:com.netops.kyy.util
 * @ClassName: DingDingUtils
 * @Version 1.0
 */
public class DingDingUtils {

    /**
     * <b>description</b> :
     * <p>使用 Token 初始化账号Client</p>
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalknotable_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalknotable_1_0.Client(config);
    }

    /**
     * <b>description</b> :
     * <p>使用 Token 初始化账号Client</p>
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkoauth2_1_0.Client createUserClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client userClient = DingDingUtils.createUserClient();
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest();
        getAccessTokenRequest.setAppKey("dinghqkbsphn0wni5nzs");
        getAccessTokenRequest.setAppSecret("gIrt59OLwEAtyGqrqtXHHkOqEbgPirFEjimM1W_RH28NhQO83OT7TneGHTtmZgiL");
        GetAccessTokenResponse getAccessTokenResponse = null;
        try {
            getAccessTokenResponse = userClient.getAccessToken(getAccessTokenRequest);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                System.out.println("code:" + err.code + " message:" + err.message);
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                System.out.println("code:" + err.code + " message:" + err.message);
            }
        }
        com.aliyun.dingtalknotable_1_0.Client client = DingDingUtils.createClient();
        com.aliyun.dingtalknotable_1_0.models.InsertRecordsHeaders insertRecordsHeaders = new com.aliyun.dingtalknotable_1_0.models.InsertRecordsHeaders();
        insertRecordsHeaders.xAcsDingtalkAccessToken = getAccessTokenResponse.body.accessToken;
        com.aliyun.dingtalknotable_1_0.models.InsertRecordsRequest insertRecordsRequest = new com.aliyun.dingtalknotable_1_0.models.InsertRecordsRequest();
        List<InsertRecordsRequest.InsertRecordsRequestRecords> records = Lists.newArrayList();
        InsertRecordsRequest.InsertRecordsRequestRecords record = new InsertRecordsRequest.InsertRecordsRequestRecords();
        Map<String, Object> fields = Maps.newHashMap();
        fields.put("发布日期", "2025-09-24");
        fields.put("发布ID", "测试");
        fields.put("视频来源", "测试");
        fields.put("视频标题", "测试");
        //fields.put("参考文案", "测试文案");
        fields.put("视频类型", "测试");
        fields.put("品项", "测试");
        fields.put("挂链产品口味", "测试");
        fields.put("视频场景", "测试");
        fields.put("视频卖点", "测试");
        fields.put("机制呈现", "测试");
        record.setFields(fields);
        records.add(record);
        insertRecordsRequest.setRecords(records);
        insertRecordsRequest.setOperatorId("K4GQhYDVy7H60fnJzvI5ggiEiE");
        try {
            client.insertRecordsWithOptions("m9bN7RYPWdlQpb77s3YXXxEYWZd1wyK0", "数据表1", insertRecordsRequest, insertRecordsHeaders, new com.aliyun.teautil.models.RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println("code:" + err.code + " message:" + err.message);
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
                System.out.println("code:" + err.code + " message:" + err.message);
            }

        }
    }
}
